﻿#region 命名空间

using System;
using System.Web.UI.WebControls;
using WinnerCMS.BLL;
using WinnerCMS.Common;
using WinnerCMS.Model;
using WinnerCMS.Model.Attribute;
using WinnerCMS.WebControls;
using WinnerCMS.WebFactory;

#endregion

namespace WinnerCMS.WebUI.Admin.ControlPanel
{
    [Access("265011")]
    public partial class Help : PageBase
    {
        private WinnerCMS.Model.Help _model = new WinnerCMS.Model.Help();
        private readonly HelpManage _myBLL = new HelpManage();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var action = Request["Action"];
                if (action == "Add")
                {
                    this.Panel1.Visible = true;
                    this.Panel2.Visible = false;
                    this.btnEdit.Visible = false;
                }
                else if (action == "Edit")
                {
                    this.Panel1.Visible = true;
                    this.Panel2.Visible = false;
                    this.btnEdit.Visible = true;
                    this.btnOK.Visible = false;


                    this._model = new HelpManage().GetInfo(int.Parse(Request["ID"]));
                    this.txtTitle.Text = this._model.Title;
                    this.cbkPass.Checked = this._model.Pass;
                    this.CE_Content.Text = this._model.Content;
                }
                else
                {
                    BindList(1);
                }
            }
        }

        private void BindList(int pageNo)
        {
            this.GV_List.EmptyDataTemplate = GetTemplate(this);


            var pp = new PagingParam();
            pp.PageNo = pageNo;
            pp.PageSize = this.PageNavigation1.PageSize;

            this.GV_List.DataSource = this._myBLL.BindList(pp);
            this.GV_List.DataBind();
            this.PageNavigation1.RecordCount = pp.RecordCount;

            if (this.GV_List.Rows.Count <= 0)
            {
                this.DelSeletedID.Enabled = false;
            }
        }

        protected void DelSeletedID_Click(object sender, EventArgs e)
        {
            this._myBLL.Delete(GetSelectedRow.GetIDs(this.GV_List), this.UpdatePanel1);
            BindList(1);
        }

        protected void PageNavigation1_PageChanging(object src, PageChangingEventArgs e)
        {
            BindList(e.NewPageIndex);
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            this._model.Id = 0;
            this._model.Title = this.txtTitle.Text.Trim();
            this._model.Admin = UserManage.Current.UserName;
            this._model.Content = this.CE_Content.Text;
            this._model.Pass = this.cbkPass.Checked;
            this._model.CheckInDate = DateTime.Now;

            var resu = this._myBLL.Add(this._model);

            if (string.IsNullOrEmpty(resu.Errors))
            {
                this.Panel1.Visible = false;
                this.Panel2.Visible = true;

                BindList(1);
            }
            else
            {
                MsgBox.Message(resu.Errors, this.UpdatePanel1);
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            this._model.Id = int.Parse(Request["ID"]);
            this._model.Title = this.txtTitle.Text;
            this._model.Admin = UserManage.Current.UserName;
            this._model.Content = this.CE_Content.Text;
            this._model.Pass = this.cbkPass.Checked;
            this._model.CheckInDate = DateTime.Now;


            var resu = this._myBLL.Add(this._model);
            if (string.IsNullOrEmpty(resu.Errors))
            {
                this.Panel1.Visible = false;
                this.Panel2.Visible = true;

                BindList(1);
            }
            else
            {
                MsgBox.Message(resu.Errors, this.UpdatePanel1);
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("Help.aspx");
        }

        protected void GV_List_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Pass")
            {
                this._myBLL.PassOrNo(this.GV_List.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString());

                BindList(this.PageNavigation1.PageNo);
            }
        }

        protected void GV_List_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            this._myBLL.Delete(this.GV_List.DataKeys[e.RowIndex].Value.ToString(), this.UpdatePanel1);
            BindList(1);
        }

        protected void GV_List_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var tc = e.Row.Cells[4];
                var lb = tc.Controls[0] as LinkButton;
                if (lb.Text == "1")
                {
                    lb.Text = "<IMG alt=\"已审核\" src=\"../Images/success-true.gif\"> ";
                }
                else
                {
                    lb.Text = "<IMG alt=\"未审核\" src=\"../Images/failed-false.gif\"> ";
                }
            }
        }
    }
}